package com.freedom.sql.jdbc.handlers;

/**
 * 
 * @author zhiqiang.liu
 * @2016年6月19日
 * @email: 837500869@qq.com
 */
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.freedom.sql.jdbc.netty.MyNioSocketChannel;
import com.freedom.sql.jdbc.pool.thread.ExecutorPool;
import com.freedom.sql.jdbc.pool.thread.MyRunnable;
import com.freedom.sql.jdbc.utils.LoggerUtils;
import com.freedom.sql.jdbc.utils.MyConstants;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
	private static final Logger logger = LogManager.getLogger(ChildChannelHandler.class);

	@Override
	protected void initChannel(SocketChannel channel) throws Exception {
		// 要在这里加上所有的处理句柄
		LoggerUtils.debug(logger, "initChannel invoked...");
		LoggerUtils.debug(logger, "channel:" + channel.getClass().getCanonicalName());
		//
		ChannelPipeline cp = channel.pipeline();
		// 第0层
		cp.addLast("LengthFieldBasedFrameDecoder", // http://asialee.iteye.com/blog/1784844
				new MyLengthFieldBasedFrameDecoder(MyConstants.MAX_PACKET_LENGTH, 0, 3, 1, 0));
		// 准备工作就绪，尝试发送Greeting Packet
		ExecutorPool.getInstance().execute(new MyRunnable(null, (MyNioSocketChannel) channel, null));
	}

}
